﻿
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using GauntletFramework.Input;
using GauntletFramework.StateManagment;
using GauntletFramework.StateManagment.Loading;

using GauntletFramework.SpriteManagment;//allow you to use the spritesheet class

namespace GauntletFramework.Games.TestGame5
{
    public class Test5 : State
    {
        public Test5()
        {
            //Load the sprite shee texture and create a sprite sheet object
            Texture2D GuyTexture = StaticGame.Content.Load<Texture2D>(@"Games\Test5\Guy");
            Sheet = new SpriteSheet(GuyTexture, 4, 4);
            
            //Create the player and add 5 animations
            Player = new BasicSprite();
            Player.AnimationList.Add("Stand", new SpriteSheetAnimation(0, 3, 0.2f, true, Sheet));
            Player.AnimationList.Add("Walk", new SpriteSheetAnimation(4, 7, 0.1f, true, Sheet));
            Player.AnimationList.Add("Glow", new SpriteSheetAnimation(8, 9, 0.3f, false, Sheet));
            Player.AnimationList.Add("Blast", new SpriteSheetAnimation(10, 12, 0.3f, false, Sheet));
            Player.AnimationList.Add("Fade", new SpriteSheetAnimation(13, 15, 0.3f, false, Sheet));

            //Set the players current animation and position
            Player.SetAnimation("Stand");
            Player.position = StaticGame.ScreenCenter;

            //add the player to the child list
            //when base.draw or base.update is called it will call the players draw or update function
            ChildList.Add(Player);
        }

        SpriteSheet Sheet;
        BasicSprite Player;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            BasicInput bi = StaticGame.BasicInput;

            //Make the player accelerate up and down
            if (bi.ArrowUp == AdvancedButtonState.Down && bi.ArrowDown != AdvancedButtonState.Down)
                Player.acceleration.Y = -100.0f;
            else if (bi.ArrowDown == AdvancedButtonState.Down && bi.ArrowUp != AdvancedButtonState.Down)
                Player.acceleration.Y = 100.0f;
            else
                Player.acceleration.Y = 0f;

            //Make the player accelerate left and right
            if (bi.ArrowLeft == AdvancedButtonState.Down && bi.ArrowRight != AdvancedButtonState.Down)
                Player.acceleration.X = -100.0f;
            else if (bi.ArrowRight == AdvancedButtonState.Down && bi.ArrowLeft != AdvancedButtonState.Down)
                Player.acceleration.X = 100.0f;
            else
                Player.acceleration.X = 0f;

            //I the player has finsihed an animation (only occurs for animations that do not loop)
            //set animation to stand
            if (Player.Current.Finished)
                Player.SetAnimation("Stand");

            //if they player in moving slower than 10 px/sec and animation is walking change to standing
            if (Player.velocity.Length() <= 10.0f && Player.GetAnimationName() == "Walk")
                Player.SetAnimation("Stand");

            //if they player in moving faster than 10 px/sec and animation is standing change to walking
            if (Player.velocity.Length() > 10.0f && Player.GetAnimationName() == "Stand")
                Player.SetAnimation("Walk");

            //if an actino button is pressed change the animation and speed of the player
            if (bi.Action1 == AdvancedButtonState.Released)
            {
                Player.acceleration = Vector2.Zero;
                Player.velocity = Vector2.Zero;
                Player.SetAnimation("Glow");
            }
            if (bi.Action2 == AdvancedButtonState.Released)
            {
                Player.acceleration = Vector2.Zero;
                Player.velocity *= 0.5f;
                Player.SetAnimation("Blast");
            }
            if (bi.Action3 == AdvancedButtonState.Released)
            {
                Player.SetAnimation("Fade");
            }

            Player.Color = new Color((float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 5) / 2f + 1f, (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 7) / 2f + 1f, (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds * 9) / 2f + 1f);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
